package com.mcc.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.system.domain.SysLogininfor;
import com.mcc.system.mapper.SysLogininforMapper;
import com.mcc.system.service.ISysLogininforService;

/**
 * 系统访问日志情况信息 服务层处理
 *
 * @author gxy
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService {

    @Autowired
    private SysLogininforMapper logininforMapper;

    /**
     * 查询系统登录日志集合
     *
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(logininfor)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(logininfor.getIpaddr()), "ipaddr", logininfor.getIpaddr());
            queryWrapper.eq(ObjectUtil.isNotEmpty(logininfor.getStatus()), "status", logininfor.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(logininfor.getUserName()), "user_name", logininfor.getUserName());
            queryWrapper.ge(ObjectUtil.isNotEmpty(logininfor.getBeginTime()), "date_format(login_time,'%Y-%m-%d')", logininfor.getBeginTime());
            queryWrapper.le(ObjectUtil.isNotEmpty(logininfor.getEndTime()), "date_format(login_time,'%Y-%m-%d')", logininfor.getEndTime());
        }
        queryWrapper.orderByDesc("info_id");
        return logininforMapper.selectList(queryWrapper);
    }

    /**
     * 新增系统登录日志
     *
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor) {
        logininforMapper.insert(logininfor);
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds) {
        if (infoIds.length == 0) return 0;
        return logininforMapper.deleteBatchIds(ListUtil.toList(infoIds));
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor() {
        logininforMapper.cleanLogininfor();
    }
}
